package com.csyf.manager.service.impl;

import com.csyf.manager.dao.*;
import com.csyf.manager.entity.*;
import com.csyf.manager.service.SysTradeSummaryService;
import com.csyf.manager.utils.DateHelper;
import com.csyf.manager.utils.ReturnData;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: Enzo
 * @Date: 2018/3/31 17:12
 */
@Service
public class SysTradeSummaryServiceImpl implements SysTradeSummaryService {

    @Autowired
    private SysTradeSummaryDao sysTradeSummaryDao;


    @Autowired
    private SysOrderProfitDao sysOrderProfitDao;

    @Autowired
    private SysTradeDao sysTradeDao;

    @Autowired
    private VAgentRateDao vAgentRateDao;

    @Autowired
    private YesProfitDao YesProfitDao;

    @Autowired
    private CurProfitDao curProfitDao;

    @Override
    public void sumYesterdayData(int day) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, -day);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(instance.getTime());
        sysTradeSummaryDao.batchInsert(format);
    }

    @Override
    public JqGridVo querydataByAgent(SysTradeSummary vo, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("trade_time desc");


        Example example = new Example(SysTradeSummary.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "agentId", vo.getAgentId());
        PageUtils.andEqualTo(criteria, "mchId", vo.getMchId());
        PageUtils.andEqualTo(criteria, "tradeTime", vo.getTradeTime());
        PageUtils.andEqualTo(criteria, "serviceType", vo.getServiceType());
        List<SysTradeSummary> select = sysTradeSummaryDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }


    @Override
    public JqGridVo queryAgentRate(VAgentRate vo, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);

        Example example = new Example(VAgentRate.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andLikeTo(criteria, "agentName", vo.getAgentName());
        PageUtils.andEqualTo(criteria, "mchId", vo.getMchId());
        PageUtils.andEqualTo(criteria, "tradeType", vo.getTradeType());
        PageUtils.andEqualTo(criteria, "status", vo.getStatus());
        List<VAgentRate> select = vAgentRateDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    @Override
    public JqGridVo querycurprofit(CurProfitVo vo, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);

        Example example = new Example(CurProfitVo.class);

        List<CurProfitVo> select = curProfitDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    @Override
    public JqGridVo queryYesprofit(YesProfitVo vo, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);

        Example example = new Example(YesProfitVo.class);

        List<YesProfitVo> select = YesProfitDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    @Override
    public JqGridVo queryCurDateTrade(SysTradeSummary vo, Integer pageIndex, Integer pageSize) {

        PageHelper.startPage(pageIndex, pageSize);
        String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
        vo.setTradeTime(Long.parseLong(format));
        List<SysTradeSummary> sysTradeSummaries = sysTradeSummaryDao.queryTrade(vo);
        return PageUtils.pasrJqGrid(sysTradeSummaries);
    }


    @Override
    public void sumprofitByDay(int day) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, -day);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(instance.getTime());
        sysTradeSummaryDao.batchPoriftInsert(format);
    }


    @Override
    public JqGridVo queryMchProfitList(SysOrderProfit vo, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("CTIME DESC,MCH_ID ASC");
        Example example = new Example(SysOrderProfit.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "mchId", vo.getMchId());
        PageUtils.andEqualTo(criteria, "srcMchId", vo.getSrcMchId());
        PageUtils.andEqualTo(criteria, "ctime", vo.getCtime());
        PageUtils.andEqualTo(criteria, "serviceType", vo.getServiceType());
        List<SysOrderProfit> select = sysOrderProfitDao.selectByExample(example);
        JqGridVo jqGridVo = PageUtils.pasrJqGrid(select);

        SysOrderProfit sysOrderProfit = sysOrderProfitDao.sumprofitData(vo);
        sysOrderProfit.setMchId(vo.getMchId());
        sysOrderProfit.setSrcMchId(vo.getSrcMchId());
        sysOrderProfit.setCtime(vo.getCtime());
        sysOrderProfit.setServiceType(vo.getServiceType());

        jqGridVo.setUserdata(sysOrderProfit);

        return jqGridVo;
    }

    @Override
    public TradeFlowVo sumTradeFlowBydate() {
        return sysTradeDao.sumTradeFlowBydate();
    }

    @Override
    public ReturnData buildChartBydate() {

        //最近7天的数组
        ArrayList<String> anyDate = DateHelper.getAnyDate(7);
        List<TradeChartVo> tradeChartVos = sysTradeDao.buildChartBydate();

        //数据为空
        if (tradeChartVos == null && tradeChartVos.size() == 0) {
            tradeChartVos = new ArrayList<>();
            for (String tradeDate : anyDate) {
                TradeChartVo vo = new TradeChartVo();
                vo.setTradeTime(tradeDate);
                vo.setTotalFee(BigDecimal.ZERO);
                tradeChartVos.add(vo);
            }
        }
        //数据少于7天
        if (tradeChartVos.size() < 7) {
            for (TradeChartVo vo : tradeChartVos) {
                for (String tradeDate : anyDate) {
                    if (tradeDate.equals(vo.getTradeTime())) {

                    }
                }

            }
        }



        return null;
    }


}
